package com.example.appjetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class TestActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
//            Greeting(name = "android")
//            NewStory()
            MaterialTheme{
//                MyScreenContent(onClick = {
//                    Toast.makeText(this@TestActivity2,"this is test",Toast.LENGTH_LONG).show()
//                })
                SimpleFilledTextFieldSample()
            }
//            MyApp{
//                Text(text = "被容器函数所修饰的 Text")
//            }
//            Counter()
//            val list= listOf("aaaa","bbbb","cccc","dddd","eeee","ffff","gggg","hhhh","iiii","jjjj","kkkk","llll","mmmm")
//            NameList(list)
//            DefaultTheme {
//                // A surface container using the 'background' color from the theme
//                Surface(
//                    modifier = Modifier.fillMaxSize(),
//                    color = MaterialTheme.colors.background
//                ) {
//                    Greeting("Android")
//                }
//            }
        }
    }
}
@Composable
fun MessageCard(name:String){
    Text(text = "hello $name")
}

@Composable
private fun Greeting(name: String) {
    Column(modifier = Modifier
        .padding(24.dp)
        .fillMaxWidth()) {
        Text(text = "Hello,")
        Text(text = name)
    }
}

@Preview
@Composable
fun NewStory() {
    Column(
        // 许多对象都有这个 Modifier 属性，这个属性非常重要，这里是设置了 padding
        modifier = Modifier.padding(16.dp)
    ) {
        Image(
            painter = painterResource(id = R.drawable.img1),
            contentDescription = null
        )
        Text("今天天气好")
        Text(text = "郑州")
        Text(text = "July 2021")
    }
}


@Composable
fun SimpleFilledTextFieldSample() {
    var text by remember { mutableStateOf("Hello") }

    TextField(
        value = text,
        onValueChange = { text = it },
        label = { Text("Label") }
    )
}


@Composable
fun MyScreenContent(names: List<String> = listOf("Android","there"),onClick:()->Unit) {
    Column(modifier = Modifier.fillMaxHeight()) {    // 类似于 match_parent
        Column(modifier = Modifier.weight(1f)) {    // 占满父布局剩余的高度空间
            for (name in names) {
                Text(text = name)
                TabRowDefaults.Divider(color = Color.Black)
            }
        }

        Button(onClick = onClick) {
            Text(text = "这是第二个 Button")
        }
        Row() {
            Text(text = "this is row1")
            Text(text = "this is row2")
        }
    }
}

@Composable
fun MyApp(content: @Composable () -> Unit) {
    MaterialTheme() {
        Surface(color = Color.Yellow, modifier = Modifier.padding(10.dp)) {
            content()
        }
    }
}

@Composable
fun Counter() {
    val count = remember { mutableStateOf(0) }  // 初始化为 0
    Button(onClick = { count.value++ }) {
        Text(text = "已点击了 ${count.value} 次！")
    }
}


@Composable
fun NameList(names: List<String>, modifier: Modifier = Modifier) {
    LazyColumn(modifier = modifier) {
        items(items = names) { name ->
            Greeting(name = name)
            TabRowDefaults.Divider(color = Color.Black)    // 分割线类对象
        }
    }
}